#include "actionfindqqlogin.h"
#include <Windows.h>

ActionFindQQLogin::ActionFindQQLogin(int w,int h,bool raise)
{
    classname = __FUNCTION__;
    actParam = QJsonObject();
    actParam.insert("width",w);
    actParam.insert("height",h);
    actParam.insert("raise",raise?1:0);
}

int ActionFindQQLogin::execute()
{
    int width = actParam.value("width").toInt();
    int height = actParam.value("height").toInt();
    bool raise = (actParam.value("raise").toInt()==1);
    HWND lastFound = NULL;
    HWND found = NULL;
    qInfo()<<classname<<"find qq login"<<width<<"x"<<height;
    do
    {
        found = ::FindWindowExA(NULL,lastFound,"TXGuiFoundation","QQ");
        if(found == NULL){
            qInfo()<<"not found TXGuiFoundation-QQ";
            return 1;
        }

        if(TRUE == ::IsWindowVisible(found)){
            //char windowTitle[100]={0};
            //GetWindowTextA(found,windowTitle,100);
            //qDebug()<<"windowTitle:"<<windowTitle;
            RECT rect;
            GetWindowRect(found,&rect);
            if(width == rect.right-rect.left &&
                    height == rect.bottom - rect.top)
            {
                actResult.insert("x",rect.left);
                actResult.insert("y",rect.top);
                actResult.insert("width",rect.right-rect.left);
                actResult.insert("height",rect.bottom-rect.top);
                ShowWindow(found,SW_SHOW);
                return 0;
            }
        }
        lastFound = found;
    }
    while(found);

    return 1;
}
